﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Drawing;
using System.Text;


public partial class System_PurviewSet : ATPages
{
    ZSql zs = new ZSql();
    Function f = new Function();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
            hidModuId.Value = Request["moduid"].ToString();
            hidId.Value = Request["id"].ToString();
            initGvData();
            initData();
 

        }
    }

    private void initGvData()
    {
        zs.Open("select * from menu");
        GvData.DataSource = zs.m_table;
        GvData.DataBind();

        //PageNavigator1.Total = 1;
        //PageNavigator1.Query = "select * from menu";
    }
    string txt = "├";
    string kg = "&nbsp;";
    protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string tempFlowId = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "url"));

            if (string.IsNullOrEmpty(tempFlowId))
            {
                e.Row.Cells[3].Visible = false;
                e.Row.Cells[2].ForeColor = Color.Red;
                //txt += e.Row.Cells[2].Text;
                //e.Row.Cells[2].Text = txt;
                e.Row.Cells[1].ForeColor = Color.Red;
                //txt += e.Row.Cells[1].Text;
                //e.Row.Cells[1].Text = txt;
            }
            else
            {
                
                e.Row.Cells[1].Text =kg+ txt + e.Row.Cells[1].Text;
                //e.Row.Cells[1].Text = txt;
            }
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        StringBuilder data = new StringBuilder();
        ZSql sel = new ZSql();
        ZSql qx = new ZSql();
        int cknum = 0;
//        string pid = "";
        int purnum = 0;
        for (int i = 0; i < this.GvData.Rows.Count; i++)
        {
            int index = Convert.ToInt32(this.GvData.Rows[i].Cells[0].Text);

            zs.Open("select * from UserPurview where moduleid=" + index + " and roleid=" + hidId.Value);
            if (zs.m_table.Rows.Count <= 0)
            {
                zs.Open("insert into UserPurview (roleid,moduleid)values(" + hidId.Value + "," + index + ")")
;

            }

            if ((this.GvData.Rows[i].Cells[3].FindControl("cbAdd") as CheckBox).Checked)
            {
                zs.Open("update UserPurview set isadd=1 where moduleid=" + index + " and roleid=" + hidId.Value);
                purnum += 1;
                cknum += 1;
            }
            else
            {
                zs.Open("update UserPurview set isadd=0 where moduleid=" + index + " and roleid=" + hidId.Value);

            }

            if ((this.GvData.Rows[i].Cells[3].FindControl("cbUpdate") as CheckBox).Checked)
            {
                zs.Open("update UserPurview set IsUpdate=1 where moduleid=" + index + " and roleid=" + hidId.Value);
                purnum += 4;
                cknum += 1;
            }
            else
            {
                zs.Open("update UserPurview set IsUpdate=0 where moduleid=" + index + " and roleid=" + hidId.Value);

            }

            if ((this.GvData.Rows[i].Cells[3].FindControl("cbDelete") as CheckBox).Checked)
            {
                zs.Open("update UserPurview set IsDel=1 where moduleid=" + index + " and roleid=" + hidId.Value);
                purnum += 2;
                cknum += 1;
            }
            else
            {
                zs.Open("update UserPurview set IsDel=0 where moduleid=" + index + " and roleid=" + hidId.Value);

            }

            if ((this.GvData.Rows[i].Cells[3].FindControl("cbFlag") as CheckBox).Checked)
            {
                zs.Open("update UserPurview set IsFlag=1 where moduleid=" + index + " and roleid=" + hidId.Value);
                purnum += 8;
                cknum += 1;
            }
            else
            {
                zs.Open("update UserPurview set IsFlag=0 where moduleid=" + index + " and roleid=" + hidId.Value);

            }
            zs.Open("select * from ModulePurview where ModuleId =" + index + " and roleid=" + hidId.Value);
            if (zs.m_table.Rows.Count <= 0)
            {
                zs.Open("insert into ModulePurview(roleid,ModuleId,Levels)values(" + hidId.Value + "," + index + "," + purnum + ")");
                purnum = 0;
            }
            else
            {
                zs.Open("update ModulePurview set Levels=" + purnum + " where roleid=" + hidId.Value + " and ModuleId=" + index);
                purnum = 0;
            }
            if (cknum > 0)
            {

                data.Append(index + ",");
            }
            cknum = 0;

        }
        string purview = data.Remove(data.Length - 1, 1).ToString();
        ZSql z = new ZSql();
        z.Open("select parentid from menu where menuid in (" + purview + ") and parentid!=0 group by parentid");
        for (int i = 0; i < z.m_table.Rows.Count; i++)
        {
            data.Insert(0, z.m_table.Rows[i]["parentid"].ToString() + ",");

        }

        purview = data.ToString();
        qx.Open("select * from purview where roleid = " + hidId.Value);
        if (!qx.NextRow())
            qx.NewRow();

        qx["roleid"] = hidId.Value;
        qx["permission"] = purview;
        qx.Update();
        z.Close();
        zs.Close();
        initData();
        Response.Redirect("Role.aspx?moduid=" + hidModuId.Value);
    }

    private void initData()
    {
        zs.Open("select * from UserPurview where roleid=" + hidId.Value);
        if (zs.m_table.Rows.Count > 0)
        {
            zs.Close();
            for (int i = 0; i < this.GvData.Rows.Count; i++)
            {
                string index = this.GvData.Rows[i].Cells[0].Text.ToString();
                if (Convert.ToBoolean(zs.GetScalar("select IsAdd from UserPurview where moduleid=" + index + " and roleid=" + hidId.Value)))
                {
                    (this.GvData.Rows[i].Cells[3].FindControl("cbAdd") as CheckBox).Checked = true;

                }

                if (Convert.ToBoolean(zs.GetScalar("select IsUpdate from UserPurview where moduleid=" + index + " and roleid=" + hidId.Value)))
                {
                    (this.GvData.Rows[i].Cells[3].FindControl("cbUpdate") as CheckBox).Checked = true;

                }
                if (Convert.ToBoolean(zs.GetScalar("select IsDel from UserPurview where moduleid=" + index + " and roleid=" + hidId.Value)))
                {
                    (this.GvData.Rows[i].Cells[3].FindControl("cbDelete") as CheckBox).Checked = true;

                }
                if (Convert.ToBoolean(zs.GetScalar("select IsFlag from UserPurview where moduleid=" + index + " and roleid=" + hidId.Value)))
                {
                    (this.GvData.Rows[i].Cells[3].FindControl("cbFlag") as CheckBox).Checked = true;

                }

            }
        }
        zs.Close();
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("User.aspx?moduid=" + hidModuId.Value);
    }
}
